// Copyright  2012 Petr TALLA. [petr.talla@gmail.com]
//
// This file is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License
// as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// This file is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY;  without even the implied warranty
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU Lesser General Public License for more details.
// [http://www.gnu.org/copyleft/lesser.html]
//=============================================================================
#include "./TcHtmlViewTabbed.h"
#include "./TcHtmlViewTab.h"
#include <TcCmdLog.h>
#include <TcCmdTransl.h>
#include <TcCmdEngine.h>
#include <TcLock.h>
#include <QByteArray>
#include <QWebFrame>
#include <QFile>

using namespace std;

//=============================================================================
TcHtmlViewTabbed::TcHtmlViewTabbed()
{
}

//=============================================================================
TcHtmlViewTabbed::~TcHtmlViewTabbed()
{
}

//=============================================================================
void TcHtmlViewTabbed::tabSet(const char* id, const char* content)
{   TcHtmlViewTab* tab = tabGet_(id);
    tab->contentSet(content);
    currentTab_ = id;
}

//=============================================================================
void TcHtmlViewTabbed::tabAdd(const char* id, const char* name)
{   TcHtmlViewTab* tab = tabGet_(id);
    tab->nameSet(name);
    if ( tabs_.size() == 1 ) currentTab_ = id;
}

//=============================================================================
TcHtmlViewTab*  TcHtmlViewTabbed::tabGet_(const char* id)
{   list<TcHtmlViewTab*>::iterator it;
    for ( it = tabs_.begin(); it != tabs_.end(); it++ )
    {   TcHtmlViewTab* tabi = *it;
        if ( strcmp(tabi->id(), id) == 0) return tabi;
    }

#ifndef NDEBUG
    string test(id);
#endif
    TcHtmlViewTab* result = new TcHtmlViewTab(id);
    tabs_.push_back(result);

    return result;
}

//=============================================================================
TcHtmlViewTabbed& TcHtmlViewTabbed::mainView(void)
{   static TcHtmlViewTabbed* view = NULL;
    if ( view == NULL )
    {   view = new TcHtmlViewTabbed();

        TcCmdEngine* engine = &TcCmdEngine::engine();
        TcCmdLog* log = new TcCmdLog(engine);
        view->log_ = log;
        TcCmdEngine::engine().append(log);

        view->resize(830,600);
        view->show();
    }
    return *view;
}

//=============================================================================
void TcHtmlViewTabbed::onEmptyQueue()
{   //<STEP> refreshing the content
    std::string cmd("tab_set_");
    cmd += currentTab_;
    EXECUTE(cmd.c_str());

    //<STEP> refreshing the content in html
    QByteArray content;

    content.append(
    "<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01//EN' 'http://www.w3.org/TR/html4/strict.dtd'>\n"
    "<html><head>\n"
    "<meta content='text/html'; charset=UTF-8'/>\n"
    "<title>TODO</title>"

    "<style type'text/css'>\n"

    "div.tab, div.content, div.separator {position: absolute; top: 0px; bottom: 0px;}\n"
    "div.content, div.separator, div.input, form, div#active, a.content:hover {background: #EEF;}\n"
    "div.tab       { left: 0px;   width: 240px; background: #8FB;}\n"
    "div.separator { left: 240px; width: 10px;}\n"
    "div.content   { left: 250px; right: 0px;}\n"
    "div.contenti, div#active  { margin-left: 12px; padding-left: 12px; padding-top: 4px; padding-bottom: 4px;}"
    "a.content:link, a.content:visited, a.content:hover, a.content:active {text-decoration: none; color: #000;}"

    "div.input {height: 30px; bottom: 0px; left: 0px; right: 0px;}\n"
    "form { left: 10px; right: 2px; top: 2px; bottom: 2px;};\n"
    "form, input, div.input { background: #F00;}\n"


    "</style>\n"

    "</head>\n"

    "<body>"
    "<div class='tab'>"
    );

    content.append("<div class='contenti'>");
    content.append("<a class='content' href='TODO'>[PREV]</a> ");
    content.append("<a class='content' href='TODO'>[NEXT]</a>&nbsp;&nbsp;&nbsp");
    content.append("<a class='content' href='TODO'>[SAVE]</a>");
    content.append("<br><br></div>");

    list<TcHtmlViewTab*>::iterator it;
    for ( it = tabs_.begin(); it != tabs_.end(); it++ )
    {   TcHtmlViewTab* tabi = *it;
        //QString tabiId(tabi->id());

        if ( currentTab_ == tabi->id() )
        {   content.append("<div id='active'>");
            content.append("<b>");
            content.append(tabi->name());
            content.append("</b>");
            content.append("</div>\n");
        }
        else
        {   content.append("<div class='contenti'>");
            content.append("<a class='content' href='tcview://#set_current_tab ");
            content.append(tabi->id());
            content.append("'>");
            content.append(tabi->name());
            content.append("</a>");
            content.append("</div>\n");
        }
    }
    content.append("</div>");

    content.append("<div class='separator'></div>");



    content.append("<div class='content'>");
    content.append(tabGet_(currentTab_.c_str())->content() );
    content.append("</div>");

    content.append("</body></html>");



    setContent(content, "", QUrl("/home"));

#ifndef NDEBUG
    QFile log("/home/buku/Development/temp/log.html");
    log.open(QIODevice::Truncate | QIODevice::WriteOnly);
    log.write(content.data(), content.count());
    log.flush();
    log.close();
#endif

    page()->mainFrame()->addToJavaScriptWindowObject("viewxx", this);

}

//=============================================================================
